package com.huanuo.internetofthings.service.resources;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.unicorn.framework.base.base.AbstractService;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.huanuo.internetofthings.bo.resources.Resources;
import com.huanuo.internetofthings.dao.resources.ResourcesMapper;
import com.huanuo.internetofthings.dto.resources.ResourcesPageRequestDto;

/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author xiebin
 * @since 2018-07-23
 */
@Service
public class ResourcesService extends AbstractService {
	@Autowired
	private ResourcesMapper resourcesMapper;

	/**
	 * 根据主键删除记录
	 * 
	 * @param id
	 * @return
	 * @throws PendingException
	 */
	public void deleteByPrimaryKey(Long id) throws PendingException {
		try {
			resourcesMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			error("删除记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "删除记录失败");
		}

	}

	/**
	 * 保存记录
	 * 
	 * @param record
	 * @return
	 * @throws PendingException
	 */
	public Resources save(Resources record) throws PendingException {
		try {
			resourcesMapper.insertSelective(record);
			return record;
		} catch (Exception e) {
			error("新增记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "新增记录失败");
		}
	}

	/**
	 * 根据主键查询记录
	 * 
	 * @param record
	 * @return
	 * @throws PendingException
	 */
	public Resources selectByPrimaryKey(Long id) throws PendingException {
		try {
			return resourcesMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			error("查询记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "查询记录失败");
		}
	}

	/**
	 * 根据条件查询记录列表
	 * 
	 * @param record
	 * @return
	 * @throws PendingException
	 */
	public List<Resources> selectList(Resources record) throws PendingException {
		try {
			return resourcesMapper.selectList(record);
		} catch (Exception e) {
			error("查询列表失败", e);
			throw new PendingException(SysCode.DB_ERROR, "查询列表失败");
		}
	}

	/**
	 * 分页查询资源表信息
	 * 
	 * @param resourcesPageRequestDto
	 * @return
	 * @throws PendingException
	 */
	public PageInfo<Resources> getResourcesByPage(ResourcesPageRequestDto resourcesPageRequestDto)
			throws PendingException {
		try {
			PageHelper.startPage(resourcesPageRequestDto.getPageNo(), resourcesPageRequestDto.getPageSize());
			List<Resources> list = resourcesMapper.selectList(resourcesPageRequestDto.getResources());
			PageInfo<Resources> pageInfo = new PageInfo<>(list);
			return pageInfo;
		} catch (Exception e) {
			error("分页查询资源表失败", e);
			throw new PendingException(SysCode.DB_ERROR, "分页查询资源表失败");
		}
	}

	/**
	 * 根据主键列表查询记录
	 * 
	 * @param record
	 * @return
	 * @throws PendingException
	 */
	public List<Resources> selectListByIds(List<Long> ids) throws PendingException {
		try {
			if (CollectionUtils.isEmpty(ids)) {
				return Lists.newArrayList();
			}
			return resourcesMapper.selectListByIds(ids);
		} catch (Exception e) {
			error("根据ID列表批量查询记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "根据ID列表批量查询记录失败");
		}
	}

	/**
	 * 根据条件查询一条记录
	 * 
	 * @param record
	 * @return
	 * @throws PendingException
	 */
	public Resources selectOne(Resources record) throws PendingException {
		try {
			return resourcesMapper.selectOne(record);
		} catch (Exception e) {
			error("查询单条记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "查询单条记录失败");
		}
	}

	/**
	 * 根据主键修改记录
	 * 
	 * @param record
	 * @return
	 * @throws PendingException
	 */
	public Resources updateByPrimaryKey(Resources record) throws PendingException {
		try {
			resourcesMapper.updateByPrimaryKey(record);
			return record;
		} catch (Exception e) {
			error("根据主键更新单条记录失败", e);
			throw new PendingException(SysCode.DB_ERROR, "根据主键更新单条记录失败");
		}
	}
}
